﻿/**
 * Code Between The Lines @ http://www.codebetweenthelines.com
 * Capture an Entire Web Page in a C# Console Application
 * Feel free to use the code but please pass the credit.
 **/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Add missing refrences
using System.Windows.Forms;
using System.Web;
using System.Drawing;
using System.Threading;

namespace CaptureWebPage
{
    class Program
    {
        static void Main(string[] args)
        {            
            CaptureWebPage capture = new CaptureWebPage(@"http://www.codebetweenthelines.com");
            capture.Capture();
            Console.ReadLine();
        }
    }

    public class CaptureWebPage
    {
        private WebBrowser browser;
        private String captureURL = @"http://www.codebetweenthelines.com";
        public String CaptureURL
        {
            get { return captureURL; }
            set { captureURL = value; }
        }

        private System.Drawing.Imaging.ImageFormat captureFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
        public System.Drawing.Imaging.ImageFormat CaptureFormat
        {
            get { return captureFormat; }
            set { captureFormat = value; }
        }

        private String saveLocation = Environment.CurrentDirectory + "\\CaptureImage.jpg";
        public String SaveLocation
        {
            get { return saveLocation; }
            set { saveLocation = value; }
        }

        public CaptureWebPage(String pageURL)
        {
            CaptureURL = pageURL;
        }


        public void Capture()
        {
            var th = new Thread(() =>
            {
                browser = new WebBrowser();
                //Register for the Document Completed Event
                browser.DocumentCompleted += webBrowserDocumentCompleted;
                browser.Navigate(CaptureURL);
                Application.Run();
            });
            
            //Set the threads appartment state to be singular
            th.SetApartmentState(ApartmentState.STA);

            //Start the thread
            th.Start();
        }


        void webBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //Set the Width and Height of the browser to match the page size acording to the scroll bars
            int webPageHeight = browser.Document.Body.ScrollRectangle.Height;
            int webPageWidth = browser.Document.Body.ScrollRectangle.Width;
            Bitmap bmp = new Bitmap(webPageWidth, webPageHeight);
            browser.Size = new Size(webPageWidth, webPageHeight);

            //Hide scroll bars so they wont show in the image capture
            browser.ScrollBarsEnabled = false;

            //capture the image from browser
            browser.DrawToBitmap(bmp, new Rectangle(browser.Location.X, browser.Location.Y, webPageWidth, webPageHeight));

            //save image
            bmp.Save(saveLocation, System.Drawing.Imaging.ImageFormat.Jpeg);
            
        }
    }
}
